/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vistas;

import entidades.Deposito;
import entidades.IngresoRepuesto;
import entidades.LoteIngresoRepuesto;
import entidades.ProveedorRepuesto;
import entidades.Repuesto;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import reportes.GenReportes;
import rn.RepuestoRN;

/**
 *
 * @author Pablo
 */
public class FormIngresoRepuestos extends javax.swing.JFrame {

    static Repuesto repuesto_selected;
    IngresoRepuesto ingresoRepuesto;
    DefaultTableModel modelo_tabla;
    List<IngresoRepuesto> ls_ingresos;

    public FormIngresoRepuestos(Repuesto rep) {
        initComponents();
        ls_ingresos = new ArrayList();
        iniciarModeloTabla();
        this.setLocationRelativeTo(null);
        this.llenarCombos();
        if (rep != null) {
            llenarCampos(rep);
        }

        this.setResizable(false);
        this.setVisible(true);

        spiner_unidades.addKeyListener(new CambiarDeCampo());
        combo_origen.addKeyListener(new CambiarDeCampo());
        tf_nro_factura.addKeyListener(new CambiarDeCampo());
        combo_proveedores.addKeyListener(new CambiarDeCampo());
        combo_depositos.addKeyListener(new CambiarDeCampo());
        bt_agregar.addKeyListener(new AgregarRepuesto());

        habilitarPanel2(false);
        bt_cerrar_ingreso.setEnabled(false);

        bt_confirmar.addKeyListener(new AccionConfirmar());
        tf_codigo_parte.addKeyListener(new buscarRepuesto());
        tf_codigo_parte.requestFocus();
        
        //Para accionar con doble clic
        tabla.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                JTable table = (JTable) me.getSource();
                Point p = me.getPoint();
                int row = table.rowAtPoint(p);
                if (me.getClickCount() == 2){
                    int id = (Integer) tabla.getValueAt(row, 0);
                    repuesto_selected = ls_ingresos.get(id).getRepuesto();
                    llerarCamposDeIngreso(ls_ingresos.get(id));                    
                    llenarCampos(repuesto_selected);                    
                    habilitarPanel2(true);                                      
                    tabla.changeSelection(row, 0, false, false);
                    quitarRepuesto();
                }
                if(me.getClickCount() == 1){
                    tabla.changeSelection(row, 0, false, false);
                    bt_quitar_de_la_tabla.setEnabled(true);                                        
                }
            }
        });
        
        
    }   
    
    private void llerarCamposDeIngreso(IngresoRepuesto ingreso){
        spiner_unidades.setValue(ingreso.getUnidades_entrantes());
        combo_origen.setSelectedItem(ingreso.getOrigen());
        tf_nro_factura.setText(ingreso.getNro_factura());
        combo_depositos.setSelectedItem(ingreso.getDeposito());
        combo_proveedores.setSelectedItem(ingreso.getProveedor());
        combo_calendario.setDate(ingreso.getFecha());
        tf_detalle.setText(ingreso.getDetalle());        
    }

    public static void llenarCampos(Repuesto r) {
        repuesto_selected = r;
        tf_codigo_parte.setText(r.getNro_parte());
        tf_descripcion_repuesto.setText(r.getDescripcion());
        tf_descripcion_repuesto.setBackground(new Color(204, 255, 255));
        tf_barcode.setText(r.getCodigo_barras());
        tf_barcode.setBackground(new Color(204, 255, 255));
        tf_marca.setText(r.getModeloRepuesto().getMarca().getDescripcion());
        tf_marca.setBackground(new Color(204, 255, 255));
        tf_modelo.setText(r.getModeloRepuesto().getDescripcion());
        tf_modelo.setBackground(new Color(204, 255, 255));
        tf_stock.setText("" + r.getStock());
        tf_stock.setBackground(new Color(204, 255, 255));
    }

    private void llenarCombos() {
        List<ProveedorRepuesto> todosProveedorRepuestos = RepuestoRN.getTodosProveedorRepuesto();
        List<Deposito> todosDeposito = RepuestoRN.getTodosDeposito();

        llenarCombo(combo_proveedores, todosProveedorRepuestos);
        llenarCombo(combo_depositos, todosDeposito);
    }

    public void llenarCombo(JComboBox combo, List lista) {
        for (Object c : lista) {
            combo.addItem(c);
        }
    }

    private class CambiarDeCampo extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
            if (e.VK_ENTER == e.getKeyCode() && e.getSource() == spiner_unidades) {
                spiner_unidades.setBackground(Color.WHITE);
                combo_origen.requestFocus();
            }
            if (e.VK_ENTER == e.getKeyCode() && e.getSource() == combo_origen) {
                String s = (String) combo_origen.getSelectedItem();
                if (s.equals("Compra")) {
                    tf_nro_factura.setBackground(new Color(204, 255, 255));
                    tf_nro_factura.requestFocus();
                } else {
                    combo_depositos.requestFocus();
                }
            }
            if (e.VK_ENTER == e.getKeyCode() && e.getSource() == tf_nro_factura) {
                tf_nro_factura.setBackground(Color.WHITE);
                combo_depositos.requestFocus();
            }
            if (e.VK_ENTER == e.getKeyCode() && e.getSource() == combo_depositos) {
                combo_proveedores.requestFocus();
            }
            if (e.VK_ENTER == e.getKeyCode() && e.getSource() == combo_proveedores) {
                bt_agregar.requestFocus();
            }
        }
    }

    private class AgregarRepuesto extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
            if (e.VK_ENTER == e.getKeyCode()) {
                if (agregarRepuesto()) {
                    habilitarPanel2(false);
                    limpiarCampos();
                    tf_codigo_parte.setEditable(true);
                    tf_codigo_parte.requestFocus();
                    bt_confirmar.setEnabled(false);
                    bt_selecionar_repuesto.setEnabled(true);
                    tf_codigo_parte.setBackground(new Color(204, 255, 255));
                }
            }
        }
    }

    public class buscarRepuesto extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
            accion(e);
        }

        public void accion(KeyEvent e) {
            tf_codigo_parte.setBackground(Color.WHITE);
            tf_info.setText(null);
            if (e.getSource() == tf_codigo_parte) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    String id_text = tf_codigo_parte.getText();
//                    if (Utils.EsUnNumeroEnteroPositivo(id_text)) {
                    //Repuesto v = RepuestoRN.findRepuestoPorID(new Long(id_text));
                    Repuesto v = RepuestoRN.findRepuestoPorCodigo(id_text);
                    if (v != null) {
                        repuesto_selected = v;
                        llenarCampos(v);
                        bt_confirmar.setEnabled(true);
                        bt_confirmar.requestFocus();
                    } else {
                        tf_info.setText("No se encuentra repuesto");
                        limpiarCampos();
                    }
                }
                if(e.VK_F2 == e.getKeyCode()){
                    new SeleccionarRepuesto(null);
                }
            }
        }
    }

    public class AccionConfirmar extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
            action(e);
        }

        private void action(KeyEvent e) {
            if (e.VK_S == e.getKeyCode() || e.VK_ENTER == e.getKeyCode()) {
                spiner_unidades.setBackground(new Color(204, 255, 255));
                confirmar();
            }
            if (e.VK_N == e.getKeyCode()) {
                tf_codigo_parte.requestFocus();
            }
        }
    }

    private void habilitarPanel2(boolean b) {
        panel2.setEnabled(b);
        spiner_unidades.setEnabled(b);
        tf_nro_factura.setEnabled(b);
        tf_detalle.setEnabled(b);
        combo_calendario.setEnabled(b);
        combo_depositos.setEnabled(b);
        combo_proveedores.setEnabled(b);
        combo_origen.setEnabled(b);
        bt_agregar.setEnabled(b);
        bt_quitar_de_la_tabla.setEnabled(b);
    }

    private void confirmar() {
        tf_codigo_parte.setEditable(false);
        bt_selecionar_repuesto.setEnabled(false);
        bt_confirmar.setEnabled(false);
        habilitarPanel2(true);
        spiner_unidades.requestFocus();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tf_codigo_parte = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tf_descripcion_repuesto = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tf_stock = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tf_marca = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tf_modelo = new javax.swing.JTextField();
        tf_barcode = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        bt_selecionar_repuesto = new javax.swing.JButton();
        bt_confirmar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        panel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        spiner_unidades = new javax.swing.JSpinner();
        jLabel9 = new javax.swing.JLabel();
        combo_origen = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        tf_nro_factura = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        combo_proveedores = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        combo_depositos = new javax.swing.JComboBox();
        jLabel14 = new javax.swing.JLabel();
        combo_calendario = new org.freixas.jcalendar.JCalendarCombo();
        jLabel11 = new javax.swing.JLabel();
        tf_detalle = new javax.swing.JTextField();
        bt_agregar = new javax.swing.JButton();
        bt_cancelar = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        bt_quitar_de_la_tabla = new javax.swing.JButton();
        bt_cerrar_ingreso = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        tf_info = new javax.swing.JLabel();
        bt_cancelar1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Repuesto"));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Código:");

        tf_codigo_parte.setBackground(new java.awt.Color(204, 255, 255));
        tf_codigo_parte.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tf_codigo_parte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_codigo_parteActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Descripcion:");

        tf_descripcion_repuesto.setEditable(false);
        tf_descripcion_repuesto.setBackground(new java.awt.Color(255, 255, 255));
        tf_descripcion_repuesto.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tf_descripcion_repuesto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_descripcion_repuestoActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Stock:");

        tf_stock.setEditable(false);
        tf_stock.setBackground(new java.awt.Color(255, 255, 255));
        tf_stock.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tf_stock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_stockActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Marca:");

        tf_marca.setEditable(false);
        tf_marca.setBackground(new java.awt.Color(255, 255, 255));
        tf_marca.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tf_marca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_marcaActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Modelo:");

        tf_modelo.setEditable(false);
        tf_modelo.setBackground(new java.awt.Color(255, 255, 255));
        tf_modelo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tf_modelo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_modeloActionPerformed(evt);
            }
        });

        tf_barcode.setEditable(false);
        tf_barcode.setBackground(new java.awt.Color(255, 255, 255));
        tf_barcode.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tf_barcode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_barcodeActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Codigo barras:");

        bt_selecionar_repuesto.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_selecionar_repuesto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_left.png"))); // NOI18N
        bt_selecionar_repuesto.setText("F2 - BUSCAR REPUESTO");
        bt_selecionar_repuesto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_selecionar_repuestoActionPerformed(evt);
            }
        });

        bt_confirmar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_confirmar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/help.png"))); // NOI18N
        bt_confirmar.setText("Confirmar? SI 'S'  - NO 'N'");
        bt_confirmar.setEnabled(false);
        bt_confirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_confirmarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(bt_selecionar_repuesto, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(bt_confirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tf_marca)
                            .addComponent(tf_codigo_parte, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tf_descripcion_repuesto, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tf_modelo, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel7)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tf_stock, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addComponent(tf_barcode, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(20, 20, 20))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_codigo_parte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_descripcion_repuesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(tf_stock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tf_marca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(tf_barcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(tf_modelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_selecionar_repuesto)
                    .addComponent(bt_confirmar))
                .addContainerGap())
        );

        jPanel2.setBackground(new java.awt.Color(0, 0, 153));

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("INGRESO DE REPUESTOS Y PARTES");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        panel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Cantidad, depósito, proveedor"));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("Cantidad:");

        spiner_unidades.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Origen:");

        combo_origen.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        combo_origen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Compra", "Otros" }));

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("Factura:");

        tf_nro_factura.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("Proveedor:");

        combo_proveedores.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel13.setText("Deposito destino:");

        combo_depositos.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel14.setText("Fecha:");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setText("Observación:");

        tf_detalle.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        bt_agregar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_agregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.png"))); // NOI18N
        bt_agregar.setText("AGREGAR");
        bt_agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_agregarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panel2Layout = new javax.swing.GroupLayout(panel2);
        panel2.setLayout(panel2Layout);
        panel2Layout.setHorizontalGroup(
            panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel2Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel11)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panel2Layout.createSequentialGroup()
                        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_nro_factura)
                            .addGroup(panel2Layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(spiner_unidades, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(combo_origen, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(45, 45, 45)
                        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel13)
                            .addComponent(jLabel12)
                            .addComponent(jLabel14))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(combo_depositos, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(combo_proveedores, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(combo_calendario, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)))
                    .addComponent(tf_detalle))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bt_agregar)
                .addContainerGap())
        );
        panel2Layout.setVerticalGroup(
            panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel2Layout.createSequentialGroup()
                        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(combo_depositos, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(spiner_unidades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel13)
                                .addComponent(jLabel8)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(combo_origen, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12)
                            .addComponent(combo_proveedores, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(combo_calendario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(tf_nro_factura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel14)
                                .addComponent(jLabel10)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tf_detalle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))
                        .addGap(13, 13, 13))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel2Layout.createSequentialGroup()
                        .addComponent(bt_agregar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        bt_cancelar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        bt_cancelar.setText("CANCELAR");
        bt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelarActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de ingresos"));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabla.setEnabled(false);
        jScrollPane1.setViewportView(tabla);

        bt_quitar_de_la_tabla.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        bt_quitar_de_la_tabla.setText("Quitar");
        bt_quitar_de_la_tabla.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_quitar_de_la_tablaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 703, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bt_quitar_de_la_tabla)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(bt_quitar_de_la_tabla)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bt_cerrar_ingreso.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cerrar_ingreso.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept.png"))); // NOI18N
        bt_cerrar_ingreso.setText("CERRAR INGRESO");
        bt_cerrar_ingreso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cerrar_ingresoActionPerformed(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel16.setText("Info:");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel16)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tf_info, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tf_info, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        bt_cancelar1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_cancelar1.setText("SALIR");
        bt_cancelar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelar1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(bt_cerrar_ingreso)
                        .addGap(18, 18, 18)
                        .addComponent(bt_cancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_cancelar1))
                    .addComponent(panel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(25, 25, 25))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_cerrar_ingreso, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_cancelar1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tf_codigo_parteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_codigo_parteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_codigo_parteActionPerformed

    private void tf_descripcion_repuestoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_descripcion_repuestoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_descripcion_repuestoActionPerformed

    private void tf_stockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_stockActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_stockActionPerformed

    private void tf_marcaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_marcaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_marcaActionPerformed

    private void tf_modeloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_modeloActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_modeloActionPerformed

    private void tf_barcodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_barcodeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_barcodeActionPerformed

    private void bt_selecionar_repuestoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_selecionar_repuestoActionPerformed
        new SeleccionarRepuesto(null);
    }//GEN-LAST:event_bt_selecionar_repuestoActionPerformed

    private void bt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelarActionPerformed
        int op = JOptionPane.showConfirmDialog(null, "¿Desea cancelar TODO el ingreso?");
        if (op == 0) {
            bt_agregar.setEnabled(false);
            bt_quitar_de_la_tabla.setEnabled(false);
            bt_confirmar.setEnabled(false);
            bt_selecionar_repuesto.setEnabled(true);
            tf_codigo_parte.setEditable(true);
            limpiarCampos();
            ls_ingresos = new ArrayList<>();
            modelo_tabla = new DefaultTableModel();
            tabla.setModel(modelo_tabla);
            iniciarModeloTabla();
            tf_codigo_parte.requestFocus();
        }
    }//GEN-LAST:event_bt_cancelarActionPerformed

    private void bt_agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_agregarActionPerformed
//        agregarRepuesto();
//        tf_codigo_parte.setBackground(new Color(204, 255, 255))
        if (agregarRepuesto()) {
            habilitarPanel2(false);
            limpiarCampos();
            tf_codigo_parte.setEditable(true);
            tf_codigo_parte.requestFocus();
            bt_confirmar.setEnabled(false);
            bt_selecionar_repuesto.setEnabled(true);
            tf_codigo_parte.setBackground(new Color(204, 255, 255));
        }
    }//GEN-LAST:event_bt_agregarActionPerformed

    private boolean agregarRepuesto() {
        blanquearCampos();
        if (repuesto_selected != null) {
            if (veficarCamposRequeridos()) {
                if (esEnteroPositivo("" + spiner_unidades.getValue())) {
                    if ((int) spiner_unidades.getValue() > 0) {
                        agregarALista();
                        limpiarCampos();
                        bt_cerrar_ingreso.setEnabled(true);
                        repuesto_selected = null;
                        return true;
                    } else {
                        JOptionPane.showMessageDialog(null, "El valor ingresado debe ser mayor a cero");
                        spiner_unidades.setBackground(Color.PINK);
                        spiner_unidades.requestFocus();
                        return false;
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "El valor ingresado no es valido");
                    spiner_unidades.setBackground(Color.PINK);
                    spiner_unidades.requestFocus();
                    return false;
                }
            } else {
                JOptionPane.showMessageDialog(null, "Hay campos requeridos");
                tf_nro_factura.setBackground(Color.PINK);
                tf_nro_factura.requestFocus();
                return false;
            }
        } else {
            JOptionPane.showMessageDialog(null, "Debe selecionar un repuesto");
            tf_codigo_parte.requestFocus();
            return false;
        }
    }


    private void bt_cerrar_ingresoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cerrar_ingresoActionPerformed
        if (!ls_ingresos.isEmpty()) {
            LoteIngresoRepuesto lote = new LoteIngresoRepuesto();
            lote.setFecha(combo_calendario.getDate());
            lote.setResponsable(Principal.usuario);
            RepuestoRN.create(lote);

            for (IngresoRepuesto ing : ls_ingresos) {
                ing.setLoteIngresoRepuesto(lote);
                RepuestoRN.create(ing);
                Repuesto repuesto = ing.getRepuesto();
                repuesto.setStock(repuesto.getStock() + ing.getUnidades_entrantes());
                RepuestoRN.edit(repuesto);
            }

            ls_ingresos = new ArrayList<>();
            modelo_tabla = new DefaultTableModel();
            tabla.setModel(modelo_tabla);
            bt_cerrar_ingreso.setEnabled(false);
            limpiarCampos();
            habilitarPanel2(false);
            JOptionPane.showMessageDialog(null, "Datos guardados!!!");
            Long ultimoLoteIngresado = RepuestoRN.getUltimoLoteIngresado();
            new GenReportes().repIngresoRepuesto(ultimoLoteIngresado);
            tf_codigo_parte.requestFocus();
        } else {
            JOptionPane.showMessageDialog(null, "No hay datos para guardar");
        }
    }//GEN-LAST:event_bt_cerrar_ingresoActionPerformed

    private void bt_quitar_de_la_tablaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_quitar_de_la_tablaActionPerformed
        quitarRepuesto();
    }//GEN-LAST:event_bt_quitar_de_la_tablaActionPerformed

    private void quitarRepuesto(){
        if (tabla.getSelectedRow() != -1) {
            int selectedRow = tabla.getSelectedRow();
            ls_ingresos.remove(selectedRow);
            modelo_tabla.removeRow(selectedRow);
            actualizarIndices();
            indice--;
            tabla.setModel(modelo_tabla);
        }
    }
    
    private void actualizarIndices(){
        int rowCount = tabla.getRowCount();
        for(int i=0; i<rowCount; i++){
            tabla.setValueAt(i, i, 0);
        }
    }
    
    private void bt_confirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_confirmarActionPerformed
        confirmar();
    }//GEN-LAST:event_bt_confirmarActionPerformed

    private void bt_cancelar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelar1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_bt_cancelar1ActionPerformed

    int indice = 0;
    private void agregarALista() {
        IngresoRepuesto ingresoRepuesto = new IngresoRepuesto();
        ingresoRepuesto.setRepuesto(repuesto_selected);
        ingresoRepuesto.setDeposito((Deposito) combo_depositos.getSelectedItem());
        ingresoRepuesto.setDetalle(tf_detalle.getText());
        ingresoRepuesto.setFecha(combo_calendario.getDate());
        ingresoRepuesto.setNro_factura(tf_nro_factura.getText());
        ingresoRepuesto.setOrigen((String) combo_origen.getSelectedItem());
        ingresoRepuesto.setProveedor((ProveedorRepuesto) combo_proveedores.getSelectedItem());
        ingresoRepuesto.setResponsable(Principal.usuario);
        ingresoRepuesto.setUnidades_entrantes((int) spiner_unidades.getValue());
        ingresoRepuesto.setPrecio_total(getMultiplicarDecimal(repuesto_selected.getPrecio_unitario(), (int) spiner_unidades.getValue()));
        ls_ingresos.add(ingresoRepuesto);
        agegarATabla(ingresoRepuesto);
    }
    
    

    private void agegarATabla(IngresoRepuesto ing) {
        Object fila[] = new Object[]{
            //ing.getRepuesto().getDescripcion(), ing.getRepuesto().getPrecio_unitario(), ing.getUnidades_entrantes(), getMultiplicarDecimal(repuesto_selected.getPrecio_unitario(), (int) spiner_unidades.getValue()), ing.getProveedor()
            indice, ing.getRepuesto(), ing.getUnidades_entrantes(), ing.getProveedor()
        };
        indice++;
        modelo_tabla.addRow(fila);
        tabla.setModel(modelo_tabla);
    }

    private void iniciarModeloTabla() {
        Object datos[][] = {};
        String[] columnNames = {"Id","Descripcion", "Cantidad", "Proveedor"};
        modelo_tabla = new DefaultTableModel(datos, columnNames);
    }

    private boolean esEnteroPositivo(String text) {
        return (text.matches("[+]?\\d*(\\\\d+)?") && text.equals("") == false);
    }

    private boolean veficarCamposRequeridos() {
        String s = (String) combo_origen.getSelectedItem();
        if (s.equals("Compra")) {
            if (tf_nro_factura.getText().isEmpty() || tf_nro_factura.getText().startsWith(" ")) {
                tf_nro_factura.setBackground(Color.PINK);
                return false;
            }
        }
        return true;
    }

    public void blanquearCampos() {
        tf_nro_factura.setBackground(Color.WHITE);
        spiner_unidades.setBackground(Color.WHITE);
    }

    private void limpiarCampos() {
        spiner_unidades.setValue(0);
        tf_barcode.setText(null);
        tf_barcode.setBackground(Color.WHITE);
        tf_descripcion_repuesto.setText(null);
        tf_descripcion_repuesto.setBackground(Color.WHITE);
        tf_detalle.setText(null);
        tf_detalle.setBackground(Color.WHITE);
        tf_marca.setText(null);
        tf_marca.setBackground(Color.WHITE);
        tf_modelo.setText(null);
        tf_modelo.setBackground(Color.WHITE);
        tf_nro_factura.setText(null);
        tf_nro_factura.setBackground(Color.WHITE);
        tf_codigo_parte.setText(null);
        tf_codigo_parte.setBackground(Color.WHITE);
        tf_stock.setText(null);
        tf_stock.setBackground(Color.WHITE);
    }

    private double getMultiplicarDecimal(double n1, int m) {
        BigDecimal suma = new BigDecimal(n1).multiply(new BigDecimal(m));
        return suma.floatValue();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_agregar;
    private javax.swing.JButton bt_cancelar;
    private javax.swing.JButton bt_cancelar1;
    private javax.swing.JButton bt_cerrar_ingreso;
    public static javax.swing.JButton bt_confirmar;
    private javax.swing.JButton bt_quitar_de_la_tabla;
    private javax.swing.JButton bt_selecionar_repuesto;
    private org.freixas.jcalendar.JCalendarCombo combo_calendario;
    private javax.swing.JComboBox combo_depositos;
    private javax.swing.JComboBox combo_origen;
    private javax.swing.JComboBox combo_proveedores;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panel2;
    private javax.swing.JSpinner spiner_unidades;
    private javax.swing.JTable tabla;
    private static javax.swing.JTextField tf_barcode;
    private static javax.swing.JTextField tf_codigo_parte;
    private static javax.swing.JTextField tf_descripcion_repuesto;
    private javax.swing.JTextField tf_detalle;
    private javax.swing.JLabel tf_info;
    private static javax.swing.JTextField tf_marca;
    private static javax.swing.JTextField tf_modelo;
    private javax.swing.JTextField tf_nro_factura;
    private static javax.swing.JTextField tf_stock;
    // End of variables declaration//GEN-END:variables

}
